package com.gxa.p2p.service.impl;

import com.gxa.p2p.common.enum1.ExceptionCode;
import com.gxa.p2p.common.exception.MyException;
import com.gxa.p2p.mapper.AgreementMapper;
import com.gxa.p2p.pojo.Agreement;
import com.gxa.p2p.service.AgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service("agreementService")
public class AgreementServiceImpl implements AgreementService {
    @Autowired
    private AgreementMapper agreementMapper=null;
    @Override
    public List<Agreement> selecctAll(Integer page, Integer size) {
        List<Agreement> maps = agreementMapper.selectAll((page - 1) * size, size);
        return maps;
    }
    @Override
    public void add(Agreement agreement) throws MyException {
        if(agreement.getAid()==null){
            agreementMapper.add(agreement);
        }else{
            throw new MyException(ExceptionCode.XIEYI_EXIST);
        }

    }

    @Override
    public void update(Long aid, Boolean astate) throws MyException {
        Agreement agreement = agreementMapper.selectById(aid);
        if(agreement!=null){
            if(!agreement.getAstate().toString().equals(astate.toString())){
                agreementMapper.update(aid, astate);
            }else{
                throw new MyException(ExceptionCode.STATE_EXIST);
            }
        }else{
            throw new MyException(ExceptionCode.NO_ID);
        }

    }

    @Override
    public Agreement selectById(Long aid) {
        Agreement agreement = agreementMapper.selectById(aid);
        return agreement;
    }

    @Override
    public List<Agreement> selectAllData() {
        return agreementMapper.selectAllData();
    }
}
